package collection;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

//List集合的演示
public class ListDemo {
    public static void main(String[] args) {
        List<String> list=new ArrayList<>();
        list.add("one");
        list.add("two");
        list.add("three");
        list.add("four");
        list.add("five");
        System.out.println(list);

        String e=list.get(2);
        System.out.println(e);

        Iterator<String> it=list.iterator();
        while (it.hasNext()){
            System.out.println(it.next());
        }
        for (String s:list){
            System.out.println(s);
        }
        //集合名.set(下标位置,新元素) 将给定元素设置到指定位置
        //list.set(2,"six");将list中下标为2的元素设置为six
        String old=list.set(2,"six");//将list下标为2的元素设置为six，并将原数据返回给old
        System.out.println("list:"+list);
        System.out.println(old);

        //list,remove(2)  删除下标为2的元素
        String s=list.remove(2);//删除下标为2的元素,并将被删除元素返回给s
        System.out.println("list:"+list);
        System.out.println(s);

        list.add(3,"seven");//在list下标为3的位置插入seven
        System.out.println(list);
    }
}
